﻿using Newtonsoft.Json;
using QWManage.Model;
using QWManage.Utils;
using System;
using System.Collections.Generic;

namespace QWManage.API.CustomerDetailB92994
{
    /// <summary>
    /// 批量获取客户详情
    /// 企业/第三方可通过此接口获取指定成员添加的客户信息列表。
    /// </summary>
    public class CustomerDetailBAPI
    {
        //https://developer.work.weixin.qq.com/document/path/92994
        //请求方式：POST（HTTPS）
        //请求地址：https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token=ACCESS_TOKEN

        /*请求示例
         {
           "userid_list":
           [
		        "zhangsan",
		        "lisi"
	        ],
           "cursor":"",
           "limit":100
        }
         */

        public static List<CustomerInfo> Invoke(string token, CustomerDetailBArgs args)
        {
            List<CustomerInfo> list = new List<CustomerInfo>();
            Root root = null;
            do
            {
                if (root != null && !string.IsNullOrEmpty(root.next_cursor))
                    args.cursor = root.next_cursor;

                string url = $"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token={token}";
                string parameters = JsonConvert.SerializeObject(args);
                string result = HttpHelper.Submit(url, parameters);
                root = JsonConvert.DeserializeObject<Root>(result);

                if (root == null || root.errcode != 0) throw new Exception("API批量获取客户详情出错");

                foreach (External_contact_list contact_list in root.external_contact_list)
                {
                    CustomerInfo userDetailBatch = new CustomerInfo();
                    userDetailBatch.Exuserid = contact_list.external_contact.external_userid;
                    userDetailBatch.Name = contact_list.external_contact.name;
                    userDetailBatch.Avatar = contact_list.external_contact.avatar;
                    userDetailBatch.Type = contact_list.external_contact.type;
                    userDetailBatch.Unionid = contact_list.external_contact.unionid;

                    userDetailBatch.F_UserID = contact_list.follow_info.userid;
                    userDetailBatch.F_Rmeark = contact_list.follow_info.remark;
                    userDetailBatch.F_Desrciption = contact_list.follow_info.description;
                    userDetailBatch.F_CreateTime = contact_list.follow_info.createtime;
                    userDetailBatch.F_Add_Way = contact_list.follow_info.add_way;
                    userDetailBatch.F_Oper_UserID = contact_list.follow_info.oper_userid;
                    string tag_ids = "";
                    if (contact_list.follow_info.tag_id != null)
                    {
                        foreach (string id in contact_list.follow_info.tag_id)
                        {
                            tag_ids += id + "\t";
                        }
                    }
                    userDetailBatch.F_Tag_IDS = tag_ids.TrimEnd('\t');
                    list.Add(userDetailBatch);
                }
            } while (root != null && !string.IsNullOrEmpty(root.next_cursor));
            return list;
        }

        public static List<CustomerTagInfo> InvokeTag(string token, CustomerDetailBArgs args)
        {
            List<CustomerTagInfo> list = new List<CustomerTagInfo>();
            Root root = null;
            do
            {
                if (root != null && !string.IsNullOrEmpty(root.next_cursor))
                    args.cursor = root.next_cursor;

                string url = $"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token={token}";
                string parameters = JsonConvert.SerializeObject(args);
                string result = HttpHelper.Submit(url, parameters);
                root = JsonConvert.DeserializeObject<Root>(result);

                if (root == null || root.errcode != 0) throw new Exception("API批量获取客户详情出错");

                foreach (External_contact_list contact_list in root.external_contact_list)
                {
                    CustomerTagInfo customerTagInfo = new CustomerTagInfo();
                    customerTagInfo.ID = contact_list.external_contact.external_userid;
                    customerTagInfo.Name = contact_list.external_contact.name;
                    customerTagInfo.TagIds = contact_list.follow_info.tag_id;
                    list.Add(customerTagInfo);
                }
            } while (root != null && !string.IsNullOrEmpty(root.next_cursor));
            return list;
        }
    }
    #region


    #endregion
    #region 参数
    public class CustomerDetailBArgs
    {
        public string[] userid_list { set; get; }
        public string cursor { set; get; }
        public int limit { set; get; }
    }
    #endregion

    #region JSON
    public class Follow_info
    {
        /// <summary>
        /// 
        /// </summary>
        public string userid { get; set; }
        /// <summary>
        /// 时光210907
        /// </summary>
        public string remark { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string description { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int createtime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> tag_id { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> remark_mobiles { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int add_way { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string oper_userid { get; set; }
    }

    public class External_contact
    {
        /// <summary>
        /// 
        /// </summary>
        public string external_userid { get; set; }
        /// <summary>
        /// 时光
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string avatar { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int gender { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string unionid { get; set; }
    }

    public class External_contact_list
    {
        /// <summary>
        /// 
        /// </summary>
        public Follow_info follow_info { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public External_contact external_contact { get; set; }
    }

    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string errmsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<External_contact_list> external_contact_list { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string next_cursor { get; set; }
    }
    #endregion
}
